package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.tiny.persistence.domain.AppUser;
import com.tiny.persistence.repository.plus.AppUserRepoPlus;

/**
 * App用户
* @ClassName: BgAppUserRepoImpl 
* @Description: TODO 
* @author:  
* @date: 2014年3月28日
*
 */
public class AppUserRepoImpl extends BaseRepoImpl<AppUser> implements AppUserRepoPlus {

	private static Logger logger = Logger.getLogger(AppUserRepoImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AppUser> getListByCondition(Map<String, Object> condition) {
		StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("from AppUser as a where 1=1 ");
        
        //查询条件
        String username = null;
        String company = null;
        if(condition.get("username")!=null){
            username = String.valueOf(condition.get("username"));
        }
        if(condition.get("company")!=null){
            company = String.valueOf(condition.get("company"));
        }
        //
        if(username!=null){
            sqlquery.append(" and a.username like :username"); 
        }
        if(company!=null){
            sqlquery.append(" and a.company like :company"); 
        }

        Query query = em.createQuery(sqlquery.toString());
        if(username!=null){
            query.setParameter("username", "%"+username+"%");
        }
        if(company!=null){
            query.setParameter("company", "%"+company+"%");
        }
        
        UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
        query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
        query.setMaxResults(uc.getPageSize());
        List<AppUser> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
	}
	
	@Override
	public int getCountByCondition(Map<String, Object> condition) {

		StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("select count(*) from AppUser as a where 1=1 ");
        
        //查询条件
        String username = null;
        if(condition.get("username")!=null){
            username = String.valueOf(condition.get("username"));
        }
        if(username!=null){
            sqlquery.append(" and a.username like :username"); 
        }

        Query query = em.createQuery(sqlquery.toString());
        if(username!=null){
            query.setParameter("username", "%"+username+"%");
        }
        
		Object obj = query.getSingleResult();
		
		logger.info("======= count=" + obj);
		
		return Integer.parseInt(obj.toString());
	}

	@SuppressWarnings("unchecked")
    public AppUser findByUsername(String username) {
        
        logger.info("RepoImpl username="+username);
        
        Query query = em.createQuery("from AppUser as au where au.username='"+username+"' ");
        List<AppUser> list = query.getResultList();
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

}
